package com.cooge.collection.ui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.cooge.collection.deal.Control;
import com.cooge.collection.pojo.CollectionUrl;
import com.cooge.collection.pojo.Video;
import com.cooge.collection.util.DataFactory;
import com.cooge.collection.util.DomUtil;

public class ShowUI extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5632501138643121386L;
	private JPanel contentPane;
	private JComboBox<Object> comboBox;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		DataFactory.init();
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ShowUI frame = new ShowUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ShowUI() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("抓取的网站");
		lblNewLabel.setBounds(10, 23, 72, 15);
		contentPane.add(lblNewLabel);

		comboBox = new JComboBox<Object>();
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == 1) {

					CollectionUrl c = (CollectionUrl) e.getItem();
					Document doc = null;
					Elements elements = null;
					{
						try {
							String url = c.getUrl().replace("{id}", "1");
							System.out.println(url);
							doc = Jsoup.connect(url).get();
							String[] s = { "div", ".item" };
							elements = DomUtil.Domanalysis(doc, s);
							if(elements.size()==0)
							Thread.sleep(1000);
						} catch (Exception e1) {

							e1.printStackTrace();
						}

					}
					while (elements.size() == 0);

					Iterator<Element> eles = elements.iterator();
					while (eles.hasNext()) {
						Element ele = eles.next();
						String[] d = { ".ititle_w", "a" };
						Elements es = DomUtil.Domanalysis(ele, d);
						//System.out.println(es.attr("title") + " " + es.attr("href"));
						String urll = es.attr("href");
						try {
							doc = Jsoup.connect(urll).get();
							
							String p = doc.getElementById("s_baidu1").attr("href");
							p =p.substring(p.lastIndexOf("pic=")+4,p.length());
							System.out.println(p);
							
							Video  v =new Video();
							String title = es.attr("title");
							
							String [] sss = title.split(" ");
							
							v.setTitle(title);
							v.setVideoAdress(doc.getElementById("link2").val());
							v.setUpdatetime(new Timestamp(new Date().getTime()));
							v.setVideoNo(sss[1]);
							v.setWebImg(p);
							Control.saveVideo(v);
							
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						
					}

				}
			}
		});
		comboBox.setBounds(78, 20, 163, 21);
		contentPane.add(comboBox);

		JButton btnNewButton = new JButton("抓数据");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

			}
		});
		btnNewButton.setBounds(331, 228, 93, 23);
		contentPane.add(btnNewButton);
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					setComboBox();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public void setComboBox() {

		List<CollectionUrl> clist = Control.getList();
		for (CollectionUrl c : clist) {
			comboBox.addItem(c);
		}
	}
}
